define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, $stateParams, T_register, GoPage, $interval, GoDialog, Auth, GoCheck, Http, GoPush, $timeout) {

		$scope.$on('$ionicView.afterEnter', function() {
			$scope.updateVerifycode();
		}, false)

		$scope.input = {
			'mobile': "",
			'password': "",
			'password2': "",
			'cellphonevcode': "",
			'seller': false //默认不选
		}
		$scope.input.thirdid = $stateParams.thirdid;
		$scope.input.thirdtype = $stateParams.thirdtype;
		$scope.showGetRegcode = true;
		$scope.regCodeString = '获取验证码';

		//上一步
		$scope.onBeforeStep = function() {
			if($scope.stepbefore) { //返回上一页
				GoPage.go_back();
			} else { //返回上一步
				$scope.stepbefore = true;
			}
		}

		//下一步
		$scope.stepbefore = true;
		$scope.onNextStep = function() {
			var input = $scope.input;
			input.mobile = (input.mobile || '').trim();
			if(!input.mobile) {
				GoDialog.alert("输入不正确", "请输入手机号码");
				return;
			}
			T_register.checkAccount({
				'mobile': input.mobile
			}, function(res) {
				console.log(res)
				if(!res.success) {
					GoDialog.alert('账号检查', res.msg);
					return;
				}
				if(res.result.needVerifyCode=='true'){
					$scope.needVerifyCode = true;//是否需要输入图片验证码
				}
				$scope.stepbefore = false;
				$scope.updateVerifycode();
			})
		}

		//获取图形验证码
		$scope.updateVerifycode = function() {
			var session = Auth.get_user_id();
			if(session == "")
				session = Auth.get_session();
			var args = {
				//				"mobile": $scope.input.mobile,
				"ts": new Date().getTime(),
				"method": 'member.verifyCodeMobile',
				"session": session
			};
			//提交数据
			var post_data = {
				sign: Http.sign(args),
				method: args.method,
				api_version: '1.0'
			};

			for(var k in args) {
				post_data[k] = args[k]
			}

			var getparam = '';
			for(var k in post_data) {
				if(!getparam) {
					getparam += '?';
				} else {
					getparam += '&';
				}
				getparam += k + '=' + encodeURIComponent(post_data[k]);
			}
			$scope.input.graphverifycode_url = HTTP_URL + getparam;
		}

		//120s倒计时--张
		$scope.otherString = 's后重新获取';
		$scope.time = "";

		function showWaitingRegcode() {
			$scope.showGetRegcode = false;
			$scope.time = 120;
			var stop = $interval(function() {
				if($scope.time > 0) {
					$scope.time = $scope.time - 1;
				} else {
					$interval.cancel(stop);
					$scope.showGetRegcode = true;
					$scope.time = '';
					$scope.regCodeString = '请重新获取';
				}
			}, 1000);
		}

		function postError() {
			GoDialog.alert('错误', "系统异常，请重新再试");
		}

		//检查填写的图形验证码
		function getCellphoneCode(mobile, verifycode) {
			if($scope.inSubmit) {
				return;
			}
			$scope.inSubmit = true;

			T_register.get_CellphoneCode({ //获取手机验证码
				'mobile': mobile,
				'verifycode': verifycode,
				"type": 'register'
			}, function(res) {
				if(!res.success) {
					GoDialog.alert('注册失败', res.msg);
					return;
				}
				showWaitingRegcode();
			}, postError, function() {
				$scope.inSubmit = false;
			})
		}

		$scope.signup = function(mobile, password, nickname, email, seller, cellphonevcode, thirdid, thirdtype) {
			T_register.signup({
				'mobile': mobile,
				'password': password,
				'nickname': nickname,
				'email': email,
				'seller': seller,
				'cellphonevcode': cellphonevcode,
				'thirdid': thirdid,
				'thirdtype': thirdtype
			}, function(res) {
				if(!res.success) {
					$scope.inSubmit = false;
					GoDialog.toast(res.msg);
					return;
				}

				GoDialog.toast('注册成功');

				//执行用户登录
				$scope.onLogin(res.result.uname, password, function() {
					//是否进行下一步
					if(seller) { //如果是卖家，则跳转卖家资料申请
						GoPage.go_states_back("t-store-data", {}, -2);
					} else { //返回到注册页面
						GoPage.go_back(-2);
					}
				});
			}, postError, function() {
				$scope.inSubmit = false;
			})
		}

		//账户登录
		$scope.onLogin = function(uname, password, callback) {
			var sourceStr = "";
			if(ionic.Platform.isIOS()) {
				sourceStr = "ios";
			} else {
				sourceStr = "android";
			}
			var param = {
				"uname": uname,
				"password": password,
				"source": sourceStr
			}
			//登录操作
			T_register.get_login(param, function(res) {
				if(!res.success) {
					GoDialog.toast(res.msg);
					return;
				}
				$timeout(function() {
					GoPush.regist();
				}, 1);
				var info = res.result;
				for(var k in info) {
					if(info[k] == null || info[k] == undefined) {
						info[k] = '';
					}
				}
				//保存用户信息在本地
				Auth.save_loginInfo(info);

				callback();
			}, function(e) {
				GoDialog.toast("Connecting error, try again...");
			}, function() {

			});
		};

		//提交表单
		$scope.onRegist = function() {
			var input = $scope.input;
			if(!input) return;

			input.mobile = (input.mobile || '').trim();
			input.cellphonevcode = (input.cellphonevcode || '').trim();
			input.nickname = (input.nickname || '').trim();
			input.email = (input.email || '').trim();
			if(!input.mobile) {
				GoDialog.alert("输入不正确", "请输入手机号码");
				return;
			} else if(!input.cellphonevcode) {
				GoDialog.alert("输入不正确", "请输入手机验证码");
				return;
			} else if(!input.password) {
				GoDialog.alert("输入不正确", "请输入新密码");
				return;
			} else if(!input.password2) {
				GoDialog.alert("输入不正确", "请输入确认密码");
				return;
			} else if(input.password != input.password2) {
				GoDialog.alert("输入不正确", "请确认两次密码输入一致");
				return;
			}
			/* else if(!input.email) {
							GoDialog.alert("输入不正确", "请输入邮箱");
							return;
						} else if(!GoCheck.checkEmail(input.email)) {
							GoDialog.alert("输入不正确", "输入的邮箱不正确");
							return;
						}*/
						
			$scope.signup(input.mobile, input.password,
				input.nickname, input.email, input.seller ? 'seller' : '',
				input.cellphonevcode, input.thirdid, input.thirdtype);
		}

		$scope.onGetRegcode = function() {
			var input = $scope.input;
			if(!input) return;

			input.mobile = (input.mobile || '').trim();
			if(!input.mobile) {
				GoDialog.alert("输入不正确", "请输入手机号码");
				$scope.inSubmit = false;
				return;
			}

			input.graphverifycode = (input.graphverifycode || '').trim();
			if(!input.graphverifycode&&$scope.needVerifyCode) {
				GoDialog.alert("输入不正确", "请输入图形验证码");
				$scope.inSubmit = false;
				return;
			}
			getCellphoneCode(input.mobile, input.graphverifycode);
		}

		//我要申请开店
		$scope.applyStore = function() {
			$scope.input.seller = !$scope.input.seller;
		}
		//密码是否可见
		$scope.inputType = "password";
		$scope.showPassword = function() {
			$scope.inputType = $scope.inputType == "password" ? "text" : "password";
		}
		//密码是否可见
		$scope.inputType2 = "password";
		$scope.showPassword2 = function() {
			$scope.inputType2 = $scope.inputType2 == "password" ? "text" : "password";
		}

	}

	controller.$inject = ['$scope', '$stateParams', 'T_register', 'GoPage', '$interval', 'GoDialog', 'Auth', 'GoCheck', 'Http', 'GoPush', '$timeout'];

	return app.registerController("t_registerctrl", controller);
});